package com.witdata.controller.community;

import com.witdata.common.domain.Result;
import com.witdata.common.domain.Status;
import com.witdata.common.domain.dto.CommunityDto;
import com.witdata.common.exception.GlobalException;
import com.witdata.common.service.community.CommunityService;
import com.witdata.common.util.IdUtils;
import lombok.RequiredArgsConstructor;
import org.springframework.util.ObjectUtils;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;

import java.util.List;

@RestController
@RequestMapping("/community")
@RequiredArgsConstructor
public class CommunityController {
    private final CommunityService communityService;
    private final IdUtils idUtils;

    @PostMapping("/create")
    public Result<String> createCommunity (
            @RequestParam("avatar") MultipartFile avatar,
            @RequestParam("name") String name,
            @RequestParam("description") String description,
            @RequestParam("title") String title
    ) {
        communityService.createCommunity(avatar, name, description, title);
        return Result.ok(Status.CREATESUCCESS);
    }

    @PostMapping("/delete/{communityId}")
    public Result<String> deleteCommunity(@PathVariable Long communityId) {

        return Result.ok();
    }

    @PostMapping("/update")
    public Result<String> updateCommunity(@RequestBody CommunityDto communityDto) {
        return Result.ok();
    }

    @GetMapping("/list")
    public Result<List<CommunityDto>> getCommunityList() {
        List<CommunityDto> communityDtoList = communityService.getCommunityList();
        return Result.ok(Status.SUCCESS, communityDtoList);
    }

    @GetMapping("/user/list")
    public Result<List<CommunityDto>> getUserCommunityList() {
        Long userId = idUtils.getUserIdByRequest();
        if (ObjectUtils.isEmpty(userId)) throw new GlobalException(Status.GetUserIdError);
        List<CommunityDto> communityDtoList = communityService.getCommunityListByUserId(userId);
        return Result.ok(Status.SUCCESS, communityDtoList);
    }

    @GetMapping("/info")
    public Result<CommunityDto> getCommunityInfo(@RequestParam("id") Long id) {
        CommunityDto communityDto = communityService.getCommunityById(id);
        return Result.ok(Status.SUCCESS, communityDto);
    }

    @PostMapping("/dissolve")
    public Result<String> dissolveCommunity(@RequestBody CommunityDto communityDto) {
        communityService.dissolveCommunity(communityDto);
        return Result.ok();
    }

}
